package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.wms.StockAPI;
import com.example.tool.erp.sdk.api.wms.dto.*;
import com.example.tool.erp.sdk.api.wms.dto.PdOrderCreateRequest.DetailDto;
import com.example.tool.erp.sdk.api.wms.dto.StockSearch2Response.Detail;
import com.example.tool.erp.sdk.api.wms.dto.StockTransferPendingResponse.ErrorInfo;
import com.example.tool.erp.sdk.impl.ApiFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.example.tool.erp.conf.HCParam.getClient;

public class Stock
{
	public static void main(String[] args) throws IOException, WdtErpException
	{
		// Client client = DefaultClient.get("wdtapi3", "test", "test");
//		Client client = DefaultClient.get("yibei3", "http://wdt.wangdian.cn", "yibei3-ota",
//				"0b3de332d42e90cd60094b4dcad8b866:959941418618631826a8ba65cad2b496");
		StockAPI stockAPI = ApiFactory.get(getClient(), StockAPI.class);

		 testStockSpecSearch(stockAPI);
		// testCreatePdOrder(stockAPI);
//		testTransferOrderCreate(stockAPI);
//		testStockSpecSearch2(stockAPI);
//		cancelStockTransferOrder(stockAPI);
//		testQueryStockShelveWithDetail(stockAPI);
//		testPendingStockTransfer(stockAPI);
	}
	public static List<StockSearchResponse.StockSearchDto> stockSpecSearch(StockAPI stockApi)
	{
		StockSearchRequest request = new StockSearchRequest();

//		request.setSpecNos(Arrays.asList("daba3", "daba4"));
//		request.setWarehouseNo("pos");
		request.setStartTime("2023-09-01");
		request.setEndTime("2023-09-20");

		StockSearchResponse response = stockApi.search(request, new Pager(11, 0, true));

		if (response.getTotal() <= 0)
		{
			System.out.println("No eligible results! ");
			return null;
		}

		List<StockSearchResponse.StockSearchDto> dtos = response.getStockSearchDtos();
		System.out.println("total: " + response.getTotal() + " first spec_no:" + dtos.get(0).getSpecNo());
		return dtos;
	}
	private static void testStockSpecSearch(StockAPI stockApi)
	{
		StockSearchRequest request = new StockSearchRequest();

//		request.setSpecNos(Arrays.asList("daba3", "daba4"));
//		request.setWarehouseNo("pos");
		 request.setStartTime("2023-09-01");
		 request.setEndTime("2023-09-20");

		StockSearchResponse response = stockApi.search(request, new Pager(11, 0, true));

		if (response.getTotal() <= 0)
		{
			System.out.println("No eligible results! ");
			return;
		}

		List<StockSearchResponse.StockSearchDto> dtos = response.getStockSearchDtos();
		System.out.println("total: " + response.getTotal() + " first spec_no:" + dtos.get(0).getSpecNo());
	}

	private static void testCreatePdOrder(StockAPI stockApi)
	{
		PdOrderCreateRequest.OrderDto orderDto = new PdOrderCreateRequest.OrderDto();
		orderDto.setDefectMode(PdOrderCreateRequest.OrderDto.DEFECT_MODE_NORMAL);
		orderDto.setWarehouseNo("1001");
		orderDto.setRemark("API TEST");

		DetailDto detailDto = new DetailDto();
		detailDto.setSpecNo("PC_2018");
		detailDto.setDefect(false);
		detailDto.setNewNum("5");
		detailDto.setRemark("detail remark");

		List<DetailDto> detailList = new ArrayList<DetailDto>();
		detailList.add(detailDto);

		Map<String, Object> response = stockApi.createPdOrder(orderDto, detailList);

		// success
		if (response != null)
			System.out.println(response);
	}

	private static void testTransferOrderCreate(StockAPI stockApi)
	{
		TransferOrderCreateRequest.orderInfoDto orderInfo = new TransferOrderCreateRequest.orderInfoDto();
		TransferOrderCreateRequest.detailDto detailDto1 = new TransferOrderCreateRequest.detailDto();
		TransferOrderCreateRequest.detailDto detailDto2 = new TransferOrderCreateRequest.detailDto();

		orderInfo.setFromWarehouseNo("lz");
		orderInfo.setToWarehouseNo("jziyy");
		orderInfo.setMode(3);
		orderInfo.setRemark("调拨单新建");
		detailDto1.setNum("1");
		detailDto1.setSpecNo("lz11");
		detailDto2.setNum("3");
		detailDto2.setSpecNo("lz13");
		List<TransferOrderCreateRequest.detailDto> detailList = new ArrayList<>();
		detailList.add(detailDto1);
		detailList.add(detailDto2);
		TransferOrderCreateResponse response = stockApi.createTransferOrder(orderInfo, detailList, true);
		response.toString();

	}

	private static void testStockSpecSearch2(StockAPI stockAPI)
	{
		StockSearch2Request request = new StockSearch2Request();

		request.setStartTime("2022-05-01 03:00:00");
		request.setEndTime("2022-05-09 04:00:00");

		StockSearch2Response response = stockAPI.search2(request, new Pager(1, 0, true));

		if (response.getTotal() <= 0)
		{
			System.out.println("No eligible results! ");
			return;
		}

		List<Detail> detailList = response.getDetailList();
		System.out.println("total: " + response.getTotal() + " first spec_no:" + detailList.get(0).getSpecNo());
	}

	private static void cancelStockTransferOrder(StockAPI api) throws WdtErpException
	{
		api.cancelStockTransferOrder("TF35339");
	}

	private static void testQueryStockShelveWithDetail(StockAPI api) throws WdtErpException
	{
		int pageSize = 5;
		Pager pager = new Pager(pageSize, 0, true);
		StockShelveQueryWithDetailRequest request = new StockShelveQueryWithDetailRequest();
		request.setStartTime("2022-03-01");
		request.setEndTime("2022-03-31");
		request.setWarehouseNo("LJLTEST");

		StockShelveQueryWithDetailResponse response = api.queryStockShelveWithDetail(request, pager);
		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.queryStockShelveWithDetail(request, pager);
			System.out.println(response.getOrderList());
		}
	}

	private static void testPendingStockTransfer(StockAPI api) throws WdtErpException
	{
		List<String> stockTransferNos = new ArrayList<>();
		stockTransferNos.add("TF35349");

		StockTransferPendingResponse response = api.pendingStockTransfer(stockTransferNos, false);
		List<ErrorInfo> errorInfoList = response.getErrorInfoList();
		if (errorInfoList.isEmpty())
		{
			System.out.println("success");
		}
		else
		{
			for (ErrorInfo errorInfo : errorInfoList)
			{
				System.out.println("异常,单号: "+errorInfo.getOrderNo()+","+errorInfo.getMessage());
			}
		}
	}
}
